<?php
			//Select grid entries
				$myTimeStamp = $_SESSION['timestamp'];
				$TimeStampPlusSevendays = ($myTimeStamp + 518400);
				$Query = "SELECT * FROM $grid_Table WHERE startTimeStamp <= '$TimeStampPlusSevendays' AND endTimeStamp >= '$myTimeStamp' ORDER BY military,mon DESC,tue DESC,wed DESC,thu DESC,fri DESC,sat DESC,sun DESC";
				$result = $db->getAll($Query, DB_FETCHMODE_ASSOC);
				if (DB::isError($result)) {
				die ($result->getMessage());
				}
				
				
			//Get air times
			$ATQuery = "SELECT * FROM $airtimes_Table ORDER BY military";
			$resultAT = $db->getAll($ATQuery, DB_FETCHMODE_ASSOC);
			if (DB::isError($resultAT)) {
				die ($resultAT->getMessage());
			}
			
						
			//POP OPEN DETAILS
			echo "<script src=\"includes/js/utility.js\"></script>\n
			<script src=\"includes/js/popup.js\"></script>\n";
			
			
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			//BEGIN DISPLAY DATA FOR GRID PLANNER
			//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
			if($browser == 'IE'){
				//javascript to submit form on pressing enter
				echo"<script language=\"JavaScript\">
				<!--
				function submitForm(event,formName){
					if(event.keyCode==13 || (event && event.which == 13)){
						document.form.submit(); 
					} 
				}
				//-->
				</script> ";
				
				echo"<form style='padding:0; margin:0;' name=\"form\" action=\"formaction.php\" method=\"post\" enctype=\"multipart/form-data\" onKeyPress=\"submitForm(event,'form');\">";
			}else{
				echo"<form style='padding:0; margin:0;' name=\"form\" action=\"formaction.php\" method=\"post\" enctype=\"multipart/form-data\">";
			}
			 
			echo"<TABLE width=\"100%\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" style=\"mainTableBGColor\">";
			//BEGINING OF FORM WHICH IS COMPLETED IN EACH ATTACHED INCLUDE BELOW
			
			echo ("<TR><TD> \n");
			//echo "<CENTER><B><font color=\"#000000\" size=\"-1\"></FONT><font color=\"$activeSectionFont\" size=\"+1\"> " . date("l - M j, Y",$_SESSION['timestamp']) . "&nbsp; &nbsp;------ thru -----&nbsp; &nbsp;" . date("l - M j, Y",($_SESSION['timestamp'] + 518400)) . "</FONT></B></CENTER> \n";
			echo("<TABLE width=\"100%\" border=\"1\" cellpadding=\"1\" cellspacing=\"0\">");
			echo("<TR class=\"blockHeader\"><TD align=\"left\" valign=\"middle\">Time</TD> <TD align=\"center\" valign=\"middle\">Mon</TD> <TD align=\"center\" valign=\"middle\">Tue</TD> <TD align=\"center\" valign=\"middle\">Wed</TD> <TD align=\"center\" valign=\"middle\">Thu</TD> <TD align=\"center\" valign=\"middle\">Fri</TD> <TD align=\"center\" valign=\"middle\">Sat</TD> <TD align=\"center\" valign=\"middle\">Sun</TD></TR> \n");
			
			
			//##################################
			//FUNCTION TO FIGURE OUR WHICH DAYS HAVE SPOTS
			//##################################
			function endQuagmire($currentPointer,$ATPointer,$myBgColor){
			$myCurrentDays = array();
			$MixedRowData = array();
			global $result;
			global $resultAT;
			global $daysOfWeek;
			
			//$i = 1;
			 $q = 0;
			while($q < 7){
			
				if($resultAT[$ATPointer]['military'] == $result[$currentPointer + $q]['military']){//check up to the next 7 days for same Time entry
					   ++$GLOBALS['pointer'];//increment $pointer for each additional row that matches curent $military except for firstone
					//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
					//GET NECESSARY ITEMS FAR ALERT WINDOW
					$MixedRowData[$q]['ID'] = $result[$currentPointer + $q]['ID'];
					$MixedRowData[$q]['pointer'] = ($currentPointer + $q);
					$MixedRowData[$q]['underwriter'] = $result[$currentPointer + $q]['underwriter'];
					$MixedRowData[$q]['spotID'] = $result[$currentPointer + $q]['spotID'];
					$MixedRowData[$q]['trade'] = $result[$currentPointer + $q]['trade'];
					$MixedRowData[$q]['rate'] = $result[$currentPointer + $q]['rate'];
					$MixedRowData[$q]['endTimeStamp'] = $result[$currentPointer + $q]['endTimeStamp'];
					$MixedRowData[$q]['startTimeStamp'] = $result[$currentPointer + $q]['startTimeStamp'];
					$MixedRowData[$q]['military'] = $result[$currentPointer + $q]['military'];
					$MixedRowData[$q]['type'] = $result[$currentPointer + $q]['type'];
					$MixedRowData[$q]['length'] = $result[$currentPointer + $q]['length'];
					$MixedRowData[$q]['mon'] = $result[$currentPointer + $q]['mon'];
					$MixedRowData[$q]['tue'] = $result[$currentPointer + $q]['tue'];
					$MixedRowData[$q]['wed'] = $result[$currentPointer + $q]['wed'];
					$MixedRowData[$q]['thu'] = $result[$currentPointer + $q]['thu'];
					$MixedRowData[$q]['fri'] = $result[$currentPointer + $q]['fri'];
					$MixedRowData[$q]['sat'] = $result[$currentPointer + $q]['sat'];
					$MixedRowData[$q]['sun'] = $result[$currentPointer + $q]['sun'];
					$MixedRowData[$q]['days'] = array();
					
						if($result[$currentPointer + $q]['mon'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"mon");
						}
						if($result[$currentPointer + $q]['tue'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"tue");
						}
						if($result[$currentPointer + $q]['wed'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"wed");
						}
						if($result[$currentPointer + $q]['thu'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"thu");
						}
						if($result[$currentPointer + $q]['fri'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"fri");
						}
						if($result[$currentPointer + $q]['sat'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"sat");
						}
						if($result[$currentPointer + $q]['sun'] == 'true'){
							array_push ($MixedRowData[$q]['days'],"sun");
						}
							
						if(count($MixedRowData[$q]['days']) > 1){
							$_SESSION['message']= $MixedRowData[$q]['spotID'] . " at " . $MixedRowData[$q]['military'] . " has more than one day asigned to it!";
						}
							
				//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
				}else{//end if
				break;
				}
				
				
			++$q;
			}//end loop	
			 
					
			//CREATE THE 7 TABLE CELLS
			$g=0;
			for ($i = 0; $i < 7; $i++) {
				
				//IF CELL HAS A SPOT ASSIGNED TO IT 
				if($MixedRowData[$g]['days'][0] == $daysOfWeek[$i]){
					echo "<TD valign=\"top\" align=\"center\" bgcolor=\"$myBgColor\" class=\"SpotLink\">
					<a href=\"#\" class=popupLink onclick=\"return !showPopup('spotInfo', event,'" . $MixedRowData[$g]['underwriter'] . "','" . $MixedRowData[$g]['spotID'] . "','";
							if($MixedRowData[$g]['trade'] == 'true'){
							    echo "trade','";
							}else {
							    echo "$" . $MixedRowData[$g]['rate'] . "','";
							}
							if($MixedRowData[$g]['endTimeStamp'] != 'NA'){
							    echo date("m/d/y" , $MixedRowData[$g]['startTimeStamp']) . " - " . date("m/d/y" , $MixedRowData[$g]['endTimeStamp']);
							}else{
							    echo date("m/d/y" , $MixedRowData[$g]['startTimeStamp']) . " - No End Date";
							}
							
							echo "','" . $MixedRowData[$g]['type'] . "','" . $MixedRowData[$g]['length'] . "','" . $MixedRowData[$g]['military'] . "','".$MixedRowData[$g]['days'][0]."','". $MixedRowData[$g]['ID'] ."');\">" . $MixedRowData[$g]['spotID'] . "</A>";
										
										
					echo"</TD>\n";
					++$g;
				//IF CELL IS EMPTY AND HAS A CHECK BOX
				}else if($resultAT[$ATPointer][$daysOfWeek[$i]] == 'true'){
					echo ("<TD valign=\"top\" align=\"center\" bgcolor=\"$myBgColor\"><INPUT name=\"". $resultAT[$ATPointer]['military']."[]\" type=\"checkbox\" value=\"". $daysOfWeek[$i] ."\"></TD>\n"); 
				
				//IF CELL IS EMPTY
				}else{
				 echo "<TD  bgcolor=\"$myBgColor\">&nbsp;</TD>\n";
				}
				
				
			}//END CREATE THE TABLE CELLS


						
			}//END FUNCTION
			//##################################
			//END FUNCTION TO FIGURE OUR WHICH DAYS 
			//##################################			
			
			
			$pointer = 0;// ARRAY POINTER FOR QUERY RESULTS
			//LOOP TO CREATE TABLE ROWS
			for ($i = 0; $i < count($resultAT); $i++) {
			
			        //Determine row color
					if($i & 1){
						$myBgColor = "#CCCCCC";
					}else{
						$myBgColor = "#FFFFFF";
					}
					
					
					//SET UP TABLE ROW AND TIME CELL
					//if ($resultAT[$i]['seconds'] == '30'){
							echo("<TR onmouseover=\"setPointer(this, $i, 'over', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '$myBgColor', '#CCFFCC', '#FFCC99');\">\n<TD bgcolor=\"$myBgColor\" valign=\"top\" align=\"left\" class=\"TimeColumn\" nowrap>" . $resultAT[$i]['hour'] . ":" . $resultAT[$i]['minute'] . ":" . $resultAT[$i]['seconds'] . " " . $resultAT[$i]['ampm'] . "</TD>\n");
					//}else{
						//if ($resultAT[$i+1]['seconds'] == '30'){
							//echo("<TR onmouseover=\"setPointer(this, $i, 'over', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '$myBgColor', '#CCFFCC', '#FFCC99');\">\n<TD bgcolor=\"$myBgColor\" valign=\"top\" align=\"left\" class=\"TimeColumn\" nowrap>" . $resultAT[$i]['hour'] . ":" . $resultAT[$i]['minute'] . " " . $resultAT[$i]['ampm'] . "</TD>\n");
						//}else{
							//echo("<TR onmouseover=\"setPointer(this, $i, 'over', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmouseout=\"setPointer(this, $i, 'out', '$myBgColor', '#CCFFCC', '#FFCC99');\" onmousedown=\"setPointer(this, $i, 'click', '$myBgColor', '#CCFFCC', '#FFCC99');\">\n<TD bgcolor=\"$myBgColor\" valign=\"top\" align=\"left\" class=\"TimeColumn\" nowrap><A href=\"index.php?ID=0&amp;subMenu=1&amp;action=update&amp;fromPlannerID=".$resultAT[$i]['ID'] ."\">" . $resultAT[$i]['hour'] . ":" . $resultAT[$i]['minute'] . " " . $resultAT[$i]['ampm'] . "</A></TD>\n");
						//}
					//}
					
					
					//CREATE GUTS OF EACH TABLE ROW
             		endQuagmire($pointer,$i,$myBgColor);
					
					
					//CLOSE UP EACH TABLE ROW
					echo "</TR>\n\n";
					


			}//end main loop
			
			
			echo "</TABLE>";
			echo ("</TD></TR> \n");
			//++++++++++++++++++++++++++++++++++++
			//END DISPLAY DATA FOR GRID PLANNER
			//++++++++++++++++++++++++++++++++++++

		//FOOTER
		echo("<TR><TD valign=\"middle\" align=\"center\" bgcolor=\"#666666\"><input name=\"PlannerStamp\" type=\"hidden\" value=\"".$_SESSION['timestamp']."\"><INPUT name=\"action\" type=\"hidden\" value=\"plannerEntry\"><INPUT name=\"Submit\" type=\"submit\" class=\"inputButton\"  value=\"Submit New Entries\"> &nbsp; &nbsp; &nbsp; <INPUT name=\"Reset\" type=\"reset\" class=\"inputButton\" value=\"Reset\"> </TD></TR>\n");
		echo("</TABLE></FORM>\n");
?>